/***************************************************
> Copyright (C) 2024 ==QWZL== All rights reserved.
> File Name: deleteInfo.c
> Author:nxq
> Mail:nxq949698473@163.com 
> Created Time: 2024年07月25日 星期四 13时16分02秒
***************************************************/

#include "WPH_server.h"
#include "viewPump.h"
#include "deleteInfo.h"


//删除信息6
void delInfo(char*buf,int sock)
{
    int num = 0;
    char type[30] = {0};
    //定义cJSON指针
    cJSON *json = NULL;
    //将字符串转为json格式对象
    json = cJSON_Parse(buf);

    cJSON *node = NULL;
    //由键获取值
    node = cJSON_GetObjectItem(json,"type");
    if(node == NULL)
    {
        LOG_E("can't find type\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(type,node->valuestring);
    }
    node = cJSON_GetObjectItem(json,"num");
    if(node == NULL)
    {
        LOG_E("can't find num\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        num = node->valueint;
    }
    
    cJSON_Delete(json);	

    char sql[100] = {0};

    MYSQL *mysql = NULL;
  	MYSQL *rs = NULL;
    mysql = mysql_init(NULL);

	rs = mysql_real_connect(mysql,"localhost","","","water",0,NULL,0);
	if(rs == NULL)
	{
		LOG_E("%s\n",mysql_error(mysql));
		mysql_close(mysql);
		exit(-1);
 	}
    int value = 0;
    //删除全部信息
    LOG_D("type = %s\n",type);
    if(strcmp(type,"delete") == 0)
    {
        if(num == 1)
        {
            strcpy(sql,"delete from wph1");
            value = 1;
        }
        else if(num == 2)
        {
            strcpy(sql,"delete from wph2");
            value = 1;
        }
        else if(num == 3)
        {
            strcpy(sql,"delete from wph3");
            value = 1;
        }
    }
    else
    {
        value = 0;
    }
    LOG_D("value = %d\n",value);
    LOG_D("sql:%s\n",sql);

 	int ret = 0;
    ret = mysql_real_query(mysql,sql,strlen(sql));
    if(ret != 0)
    {
        LOG_E("%s\n",mysql_error(mysql));
        mysql_close(mysql);
        exit(-1);
    }
    mysql_close(mysql);

    //发送
    ret = send(sock,&value,4,0);
    if(ret == -1)
    {
        LOG_E("send\n");
        exit(-1);
    }
    close(sock);
}
